package com.bcmusic.distri.service.impl;

import com.bcmusic.distri.dao.DistributeConstractMapper;
import com.bcmusic.distri.pojo.DistributeConstract;
import com.bcmusic.distri.service.DistributeConstractService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:DistributeConstract业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class DistributeConstractServiceImpl implements DistributeConstractService {

    @Autowired
    private DistributeConstractMapper distributeConstractMapper;


    /**
     * DistributeConstract条件+分页查询
     * @param distributeConstract 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<DistributeConstract> findPage(DistributeConstract distributeConstract, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(distributeConstract);
        //执行搜索
        return new PageInfo<DistributeConstract>(distributeConstractMapper.selectByExample(example));
    }

    /**
     * DistributeConstract分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<DistributeConstract> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<DistributeConstract>(distributeConstractMapper.selectAll());
    }

    /**
     * DistributeConstract条件查询
     * @param distributeConstract
     * @return
     */
    @Override
    public List<DistributeConstract> findList(DistributeConstract distributeConstract){
        //构建查询条件
        Example example = createExample(distributeConstract);
        //根据构建的条件查询数据
        return distributeConstractMapper.selectByExample(example);
    }


    /**
     * DistributeConstract构建查询对象
     * @param distributeConstract
     * @return
     */
    public Example createExample(DistributeConstract distributeConstract){
        Example example=new Example(DistributeConstract.class);
        Example.Criteria criteria = example.createCriteria();
        if(distributeConstract!=null){
            // 音乐分发合约id
            if(!StringUtils.isEmpty(distributeConstract.getConstractId())){
                    criteria.andEqualTo("constractId",distributeConstract.getConstractId());
            }
            // 分发商编号。关联distributor表的distributor_number字段。
            if(!StringUtils.isEmpty(distributeConstract.getDistributorNumber())){
                    criteria.andEqualTo("distributorNumber",distributeConstract.getDistributorNumber());
            }
            // 对应music_distribution 表的id
            if(!StringUtils.isEmpty(distributeConstract.getDistributionId())){
                    criteria.andEqualTo("distributionId",distributeConstract.getDistributionId());
            }
            // 分发商MB分配比（=分发商自持MB分配比+终端用户MB分配比）。与管理员设定的MB交易手续分配比一致，从whole_asset表获得。比例值均乘以100保存。
            if(!StringUtils.isEmpty(distributeConstract.getDistributorRatio())){
                    criteria.andEqualTo("distributorRatio",distributeConstract.getDistributorRatio());
            }
            // 分发商自持MB分配比。由签约的分发商自行设定。
            if(!StringUtils.isEmpty(distributeConstract.getDistributorOwnRatio())){
                    criteria.andEqualTo("distributorOwnRatio",distributeConstract.getDistributorOwnRatio());
            }
            // 终端用户MB分配比。
            if(!StringUtils.isEmpty(distributeConstract.getEndUserRatio())){
                    criteria.andEqualTo("endUserRatio",distributeConstract.getEndUserRatio());
            }
            // 签约时间。即合约生成时间。
            if(!StringUtils.isEmpty(distributeConstract.getCreateTime())){
                    criteria.andEqualTo("createTime",distributeConstract.getCreateTime());
            }
            // 1为有效，0为无效
            if(!StringUtils.isEmpty(distributeConstract.getValidStatus())){
                    criteria.andEqualTo("validStatus",distributeConstract.getValidStatus());
            }
            // 是否开启分发，1表示开启，0表示不
            if(!StringUtils.isEmpty(distributeConstract.getOpenStatus())){
                    criteria.andEqualTo("openStatus",distributeConstract.getOpenStatus());
            }
            //
            if(!StringUtils.isEmpty(distributeConstract.getUpdateTime())){
                    criteria.andEqualTo("updateTime",distributeConstract.getUpdateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        distributeConstractMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改DistributeConstract
     * @param distributeConstract
     */
    @Override
    public void update(DistributeConstract distributeConstract){
        distributeConstractMapper.updateByPrimaryKey(distributeConstract);
    }

    /**
     * 增加DistributeConstract
     * @param distributeConstract
     */
    @Override
    public void add(DistributeConstract distributeConstract){
        distributeConstractMapper.insert(distributeConstract);
    }

    /**
     * 根据ID查询DistributeConstract
     * @param id
     * @return
     */
    @Override
    public DistributeConstract findById(Integer id){
        return  distributeConstractMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询DistributeConstract全部数据
     * @return
     */
    @Override
    public List<DistributeConstract> findAll() {
        return distributeConstractMapper.selectAll();
    }
}
